<template>
  <div class="role-container">
    <div class="left-wrapper">
      <div
        v-for="(item, index) in rolelist"
        :key="item.roleId"
        class="role-item"
        @click="select(index)"
        :class="cur == index ? 'ac' : ''"
      >
        <div class="role-info">
          <svg-icon icon-class="user" class="icon" />
          <el-tooltip
            class="item"
            effect="dark"
            :content="item.remark"
            placement="top"
          >
            <div>{{ item.roleName }}</div>
          </el-tooltip>
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="editrole(item.roleId)"
                >编辑角色</el-dropdown-item
              >
              <el-dropdown-item @click.native="shanchu(item.roleId)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" @click="addrole" size="mini"
        >添加角色</el-button
      >
    </div>
    <div class="right-wrapper">
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in list" :key="item.id" class="tree-item">
              <div class="tree-title">{{ item.title }}</div>
              <el-tree
                :default-expand-all="true"
                :data="item.children"
                ref="tree"
                node-key="id"
                show-checkbox
                :default-checked-keys="checkkey"
                :props="defaultProps"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员列表(${userTotal})`" name="member">
          <div class="table">
            <el-table :data="tableData" style="width: 100%">
              <el-table-column type="index" label="序号" />
              <el-table-column label="员工姓名" prop="name" />
              <el-table-column label="登录账号" prop="userName" />
            </el-table>
            <el-pagination
              layout="total, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="form.page"
              :page-size="form.pageSize"
            />
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import { roledelete, roleid, rolelb, rolelist, roleuser } from "@/api/user";

export default {
  name: "Role",
  data() {
    return {
      rolelist: [],
      list: [],
      userTotal: 0,
      activeName: "tree",
      defaultProps: {
        children: "children",
        label: "title",
      },
      form: {
        page: 1,
        pageSize: 10,
      },
      checkkey: [],
      tableData: [],
      total: 0,
      cur: 0,
      perm: [],
      roleid: "",
    };
  },
  mounted() {
    this.getdata();
    this.gettree();
  },
  methods: {
     shanchu(id) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await roledelete(id);
          if (res.code == 10000) {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getdata();
          } else {
            this.$message({
              type: "info",
              message: res.msg,
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addrole() {
      this.$router.push({
        path: "/addrole",
      });
    },
    editrole(roleId) {
      this.$router.push({
        path: "/addrole",
        query: {
          roleId: roleId,
        },
      });
    },
    select(index) {
      this.cur = index;
      this.roleid = this.rolelist[index].roleId;
      this.gettreedata(this.roleid);
      this.getroleuser(this.roleid);
    },
    handleSizeChange(e) {
      this.form.pageSize = e;
      this.getroleuser(this.roleid);
    },
    handleCurrentChange(e) {
      this.form.page = e;
      this.getroleuser(this.roleid);
    },
    async gettreedata(id) {
      const res = await roleid(id);
      console.log(res, "res");
      this.userTotal = res.data.userTotal;
      this.perm = res.data.perms;
      this.$refs.tree.forEach((el, index) => {
        el.setCheckedKeys(this.perm[index]);
      });
    },
    async gettree() {
      const res = await rolelist();
      console.log(res, "resss");
      this.list = res.data;
      this.adddisable(this.list);
    },
    adddisable(list) {
      list.forEach((el) => {
        el.disabled = true;
        if (el.children) {
          this.adddisable(el.children);
        }
      });
    },
    async getroleuser(roleid) {
      const res = await roleuser(roleid, this.form);
      console.log(res, "res1");
      this.tableData = res.data.rows;
      this.total = res.data.total;
    },
    async getdata() {
      const res = await rolelb();
      console.log(res, "res");
      this.rolelist = res.data;
      this.roleid = this.rolelist[0].roleId;
      this.gettreedata(this.roleid);
      this.getroleuser(this.roleid);
    },
  },
};
</script>

<style>
.ac {
  background: #edf5ff;
  color: #4770ff;
}
</style>
<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn {
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper {
      padding: 20px;
    }
  }
}
</style>